﻿using ArtGalleryApp.Data;
using ArtGalleryApp.DataModels;
using ArtGalleryApp.Web.IDataAdapters.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace ArtGalleryApp.Web.IDataAdapters.Adapters
{
    public class ArtGalleryAdapter : IArtGalleryAdapter
    {
        public List<ArtPiece> GetAllArt()
        {
            List<ArtPiece> manifest;
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                manifest = db.ArtPieces.OrderBy(x=>x.Name).ToList();
            }
            return manifest;
        }

        public ArtPiece GetArtPiece(int id)
        {
            ArtPiece work;
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                work = db.ArtPieces.Find(id);
            }
            return work;
        }

        public ArtPiece UpdateArtPiece(ArtPiece artpiece)
        {
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                ArtPiece ap = db.ArtPieces.Find(artpiece.Id);
                ap.setProps(artpiece);
                db.SaveChanges();
            }
            return artpiece;
        }

        public int AddArtPiece(ArtPiece artpiece)
        {
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                artpiece = db.ArtPieces.Add(artpiece);
                db.SaveChanges();
            }
            return artpiece.Id;
        }

        public void DeleteArtPiece(int id)
        {
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                ArtPiece ap = db.ArtPieces.Find(id);
                db.ArtPieces.Remove(ap);
                db.SaveChanges();
            }
        }
    }
}